<template>
  <div class="wjhuishou-list">
    <div class="search-box">
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="文件编号">
          <el-input v-model="searchForm.fileNumber" placeholder="请输入文件编号"></el-input>
        </el-form-item>
        <el-form-item label="文件名称">
          <el-input v-model="searchForm.fileName" placeholder="请输入文件名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="resetSearch">重置</el-button>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="fileNumber" label="文件编号" width="180"></el-table-column>
      <el-table-column prop="fileName" label="文件名称"></el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180"></el-table-column>
      <el-table-column label="操作" width="200" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleView(scope.row)">查看</el-button>
          <el-button size="mini" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.current"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getKaoqinWenjianPage, deleteKaoqinWenjian } from '@/api/kaoqinWenjian'

export default {
  name: 'WjhuishouList',
  data() {
    return {
      searchForm: {
        fileNumber: '',
        fileName: ''
      },
      tableData: [],
      page: {
        current: 1,
        size: 10,
        total: 0
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    async fetchData() {
      try {
        const { data } = await getKaoqinWenjianPage({
          current: this.page.current,
          size: this.page.size,
          fileNumber: this.searchForm.fileNumber,
          fileName: this.searchForm.fileName
        })
        this.tableData = data.data.records
        this.page.total = data.data.total
      } catch (error) {
        console.error('获取数据失败:', error)
      }
    },
    handleSearch() {
      this.page.current = 1
      this.fetchData()
    },
    resetSearch() {
      this.searchForm = {
        fileNumber: '',
        fileName: ''
      }
      this.handleSearch()
    },
    handleSizeChange(val) {
      this.page.size = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.page.current = val
      this.fetchData()
    },
    handleAdd() {
      this.$router.push({
        name: 'Wjhuishou',
        query: { type: 'add' }
      })
    },
    handleView(row) {
      this.$router.push({
        name: 'Wjhuishou',
        query: { id: row.id, type: 'view' }
      })
    },
    handleEdit(row) {
      this.$router.push({
        name: 'Wjhuishou',
        query: { id: row.id, type: 'edit' }
      })
    },
    handleDelete(row) {
      this.$confirm('确认删除该记录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          await deleteKaoqinWenjian(row.id)
          this.$message.success('删除成功')
          this.fetchData()
        } catch (error) {
          console.error('删除失败:', error)
          this.$message.error('删除失败')
        }
      }).catch(() => {})
    }
  }
}
</script>

<style scoped>
.wjhuishou-list {
  padding: 20px;
}
.search-box {
  margin-bottom: 20px;
}
.pagination-container {
  margin-top: 20px;
  text-align: right;
}
</style> 